package com.reyco1.medusa.display.control
{
	import com.reyco1.medusa.display.SmartSprite;
	import com.reyco1.medusa.display.graphics.Box;
	import com.reyco1.medusa.util.AlignUtil;
	import com.reyco1.medusa.util.Delegate;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.Font;
	import flash.ui.Mouse;

	public class SimpleColorButton extends BaseControl
	{
		[Embed(source="../assets/fonts/FUTURA_MEDIUM.TTF", embedAsCFF="false", fontName="Arial", mimeType="application/x-font")]
		public static var ARIAL:Class;
		
		private var background:SmartSprite;
		private var label:Label;
		private var strLabel:String;
		private var backColor:uint;
		private var labelColor:uint;
		private var onClick:Function;
		private var onClickParams:Array;
		
		public function SimpleColorButton(label:String, color:uint = 0x00FF00, labelColor:uint = 0xFFFFFF, onClick:Function = null, onClickParams:Array = null)
		{
			super();
			
			strLabel 			= label;
			backColor 			= color;
			
			this.labelColor 	= labelColor;
			this.onClick 		= onClick;
			this.onClickParams 	= onClickParams;
			
			addEventListener(MouseEvent.CLICK, handleClick);
			
			buttonMode = useHandCursor = true;
			
			build();
		}
		
		override protected function build():void
		{
			removeAllChildren();
			
			if(label) label = null;
			if(background) background = null;
			
			var arialFont:Font = new ARIAL() as Font;
			
			label = new Label(strLabel, arialFont.fontName, 10, labelColor, true, true, 14, true);
			background = Box.drawGradiant(0, 0, label.width + 10, label.height, [backColor, backColor/1.5], [1, 1], [0, 255]);
			
			_height = background.height;
			_width  = background.width;
			
			addChild(background);
			addChild(label);
			
			AlignUtil.middleCenter(background, label);
			
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		override protected function onRemoved():void
		{
			removeAllListeners();			
			removeAllChildren();			
			if(label) 
				label = null;
			if(background) 
				background = null;
		}
		
		/* handlers */
		
		private function handleClick(e:MouseEvent):void
		{
			if(onClick != null) Delegate.execute(onClick, onClickParams);
		}
	}
}